#!/bin/bash
set -e

# Set our DNS requirements
TTL=300
id=$(cut -d/ -f2 <<< $JUJU_UNIT_NAME)
ip=$(unit-get public-address)
relation-set domain=$(config-get zone)
relation-set resources='@ '$TTL' IN A '$ip'
@ '$TTL' IN NAPTR 1 1 "S" "SIP+D2T" "" _sip._tcp
@ '$TTL' IN NAPTR 2 1 "S" "SIP+D2U" "" _sip._udp
_sip._tcp '$TTL' IN SRV 0 0 5060 bono-'$id'
_sip._udp '$TTL' IN SRV 0 0 5060 bono-'$id'
bono-'$id' '$TTL' IN A '$ip

# Update our DNS server
if [ "$(relation-get private-address)" != "" ]
then
  echo nameserver $(relation-get private-address) > /etc/dnsmasq.resolv.conf
  grep -v ^RESOLV_CONF= /etc/default/dnsmasq > /tmp/dnsmasq.$$
  mv /tmp/dnsmasq.$$ /etc/default/dnsmasq
  echo RESOLV_CONF=/etc/dnsmasq.resolv.conf >> /etc/default/dnsmasq
  service dnsmasq restart
fi

# Update Clearwater configuration and restart
$CHARM_DIR/lib/config_script programmable-multiple
$CHARM_DIR/lib/restart
